/*
 * @Author: qsy
 * @Date: 2024-12-28 14:50:06
 * @LastEditors: qsy
 * @LastEditTime: 2024-12-28 15:05:11
 * @Description: spi 驱动
 */
/*********************************************************************
 * INCLUDES
 */
#include <string.h>
#include "driver/spi_master.h"
#include "esp_log.h"

#include "board_spi.h"

/*********************************************************************
 * INCLUDES
 */
#include <string.h>
#include "driver/spi_master.h"

#include "board_spi.h"

/*********************************************************************
 * LOCAL VARIABLES
 */
static spi_device_handle_t s_spiHandle;

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief ADXL345 SPI驱动初始化
 @param 无
 @return 无
*/
void ADXL345_SPI_Init(void)
{
    esp_err_t ret;

    spi_bus_config_t buscfg = {
        .miso_io_num = ADXL345_SPI_MISO_PIN,
        .mosi_io_num = ADXL345_SPI_MOSI_PIN,
        .sclk_io_num = ADXL345_SPI_SCLK_PIN,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 0
    };

    spi_device_interface_config_t devcfg = {
        .clock_speed_hz = 1000000,    // 1MHz
        .mode = 3,                    // SPI mode 3
        .spics_io_num = ADXL345_SPI_CS_PIN,
        .queue_size = 7,
    };

    // 使用 SPI_DMA_CH_AUTO 代替具体的通道号
    ESP_ERROR_CHECK(spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
    ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST, &devcfg, &s_spiHandle));

    // 配置CS引脚
    gpio_config_t io_conf;  // 定义一个gpio_config类型的结构体，下面的都算对其进行的配置
    io_conf.intr_type = GPIO_INTR_DISABLE;  	// 禁止中断
    io_conf.mode = GPIO_MODE_INPUT;             // 选择输入模式
    io_conf.pin_bit_mask = (1ULL<<ADXL345_SPI_CS_PIN);  // 配置GPIO_IN寄存器
    io_conf.pull_down_en = 0;                   // 禁止下拉
    io_conf.pull_up_en = 0;                     // 禁止上拉
    gpio_config(&io_conf);                      // 最后配置使能

    gpio_set_direction(ADXL345_SPI_CS_PIN, GPIO_MODE_OUTPUT);// 把这个GPIO作为输出
}

/**
 @brief ADXL345 SPI写入数据
 @param pData -[in] 写入数据
 @param dataLen -[in] 写入数据长度
 @return 无
*/
void ADXL345_SPI_Write(uint8_t *pData, uint32_t dataLen)
{
    esp_err_t ret;
    spi_transaction_t t;
    if(0 == dataLen)                                        // no need to send anything
    {
        return;
    }

    memset(&t, 0, sizeof(t));                               // Zero out the transaction
    t.length = dataLen * 8;                                 // Len is in bytes, transaction length is in bits.
    t.tx_buffer = pData;                                    // Data
    ret = spi_device_polling_transmit(s_spiHandle, &t);     // Transmit!
    assert(ret == ESP_OK);                                  // Should have had no issues.
}

/**
 @brief ADXL345 SPI读取数据
 @param pData -[out] 读取数据
 @param dataLen -[in] 读取数据长度
 @return 无
*/
void ADXL345_SPI_Read(uint8_t *pData, uint32_t dataLen)
{
    spi_transaction_t t;
    if(0 == dataLen)                                        // no need to receivce anything
    {
        return;
    }

    memset(&t, 0, sizeof(t));                               // Zero out the transaction
    t.length = dataLen * 8;                                 // Len is in bytes, transaction length is in bits.
    t.rx_buffer = pData;
    esp_err_t ret = spi_device_polling_transmit(s_spiHandle, &t);
    assert(ret == ESP_OK);
}

/**
 @brief ADXL345 SPI CS引脚设置
 @param level -[in] 电平
 @return 无
*/
void ADXL345_SPI_CS_Set(uint8_t level)
{
	gpio_set_level(ADXL345_SPI_CS_PIN, level);
}

/****************************************************END OF FILE****************************************************/
